package client.ui.view.dialogview;

import client.UserState;
import client.assets.Icons;
import client.control.Controller;
import client.control.ResponseEntity;
import client.ui.component.button.MaterialButton;
import client.ui.component.edittext.TextEdit;
import client.ui.view.base.PanelControl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class AuctionDialog {
    public static void showAuctionDialog(Frame owner,Container parentContainer, int goodId){
        Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
        JDialog dialog = new JDialog(owner,true);
        dialog.setSize(350,300);
        dialog.setResizable(false);
        dialog.setLocationRelativeTo(parentContainer);
        dialog.setTitle("设置拍卖");
        dialog.setIconImage(Icons.auction.getImage());

        JButton confirm = MaterialButton.BlueButton("确认",null);
        TextEdit startTime = new TextEdit("请输入拍卖开始时间(yyyy-MM-dd)");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        startTime.setText(simpleDateFormat.format(new Date()));
        TextEdit endTime = new TextEdit("请输入拍卖截止时间(yyyy-MM-dd)");
        endTime.setText(simpleDateFormat.format(new Date()));
        TextEdit maxPrice = new TextEdit("请输入起拍价(￥)");

        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date start;
                Date end;
                double price;
                //格式化并判断数据合法性
                try{
                    start = format.parse(startTime.getText());
                } catch (ParseException ex) {
                    startTime.setErrorText("请检查日期格式(yyyy-mm-dd)");
                    return;
                }
                try{
                    end = format.parse(endTime.getText());
                } catch (ParseException ex) {
                    endTime.setErrorText("请检查日期格式(yyyy-mm-dd)");
                    return;
                }
                if (start.getTime() > end.getTime()){
                    endTime.setErrorText("结束时间不能早于起始时间");
                    return;
                }
                try {
                    price = Double.parseDouble(maxPrice.getText());
                    if (price < 0){
                        throw new Exception();
                    }
                } catch (Exception e1){
                    maxPrice.setErrorText("请输入合理的价格");
                    return;
                }
                //置空错误
                startTime.setErrorText("");
                endTime.setErrorText("");
                maxPrice.setErrorText("");
                double finalPrice = price;
                singleThreadExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        ResponseEntity responseEntity =
                                Controller.AUCTION_CONTROL.addAuction(UserState.getToken(), goodId, startTime.getText(), endTime.getText(), finalPrice);
                        if (responseEntity.getCode() == 0){
                            SwingUtilities.invokeLater(new Runnable() {
                                @Override
                                public void run() {
                                    dialog.dispose();
                                    JOptionPane.showMessageDialog(owner,"创建成功");
                                    PanelControl.panels.get("GoodsPanel");
                                }
                            });
                        } else {
                            SwingUtilities.invokeLater(new Runnable() {
                                @Override
                                public void run() {
                                    JOptionPane.showMessageDialog(owner,"创建失败");
                                }
                            });
                        }
                    }
                });
            }
        });

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        Box vBox = Box.createVerticalBox();
        vBox.add(startTime);
        vBox.add(endTime);
        vBox.add(maxPrice);
        vBox.add(new JLabel(" "));
        vBox.add(confirm);

        panel.add(vBox);
        dialog.setContentPane(panel);

        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }
}
